<template>
  <el-dialog
    append-to-body
    title="店铺满减"
    :visible.sync="dialogVisible"
    width="500px">
    <div align="left">
      <el-tag size="small" type="success" v-for="(item,index) in arr" :key="index" style="margin: 5px" :closable="true" @close="checkArr(index)">{{ '满'+item.full+',减'+item.minus }}</el-tag>
    </div>
    <div align="left">
      <el-input style="width: 320px" v-model="currString" placeholder="请输入,如: '100,10' 就是满100减10,注意'满'不能重复" @keyup.enter.native="submit"></el-input>
      <el-button type="success" @click="submit">添加</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'FullMinusDialog',
  props:{
    callback:Function
  },
  data:function () {
    return {
      currString:'',
      arr:[
        {full:100,minus:5},
      ],
      dialogVisible:false
    }
  },
  mounted () {
    this.arr.sort(function (a,b) {
      return b.full-a.full
    })
  },
  methods:{
    checkArr:function (index) {
      this.$delete(this.arr,index)
      console.log(this.arr)
    },
    submit:function () {
      if (this.arr.length>=10){
        return this.$message.error("最多添加10条")
      }
      var arr=this.currString.split(',')
      if (arr.length!==2){
        return this.$message.error('输入 错误')
      }
      arr[0]-=0
      arr[1]-=0
      if (arr[0]<=0 || arr[1]<=0 || (arr[0]<=arr[1])){
        return this.$message.error('输入数值 错误')
      }
      var isAdd=false
      this.arr.forEach(function (value,index){
        if (value.full===arr[0]){
          isAdd=true
        }
      })
      if (isAdd){
        return this.$message.warning("已经添加 "+arr[0]+' 这个满足条件')
      }
      this.arr.push({
        full:arr[0],
        minus:arr[1],
      })
      this.currString=''
      return this.$message.success('添加成功')
    },
    open:function (paramsArr) {
      this.arr=paramsArr
      this.dialogVisible=true
      this.$forceUpdate()
    },
    confirm:function () {
      this.$props.callback(this.arr)
      this.dialogVisible=false
    }
  }
}
</script>

<style scoped>

</style>
